package com.jeesite.modules.sys.wx.fw.utils;

import com.github.sd4324530.fastweixin.api.TemplateMsgAPI;
import com.github.sd4324530.fastweixin.api.config.ApiConfig;
import com.github.sd4324530.fastweixin.api.entity.TemplateMsg;
import com.github.sd4324530.fastweixin.api.entity.TemplateParam;
import com.github.sd4324530.fastweixin.api.response.SendTemplateResponse;
import com.jeesite.common.config.Global;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 结果通知模板工具
 */
@Component
public class ResultTemplateMsgUtil {
    /**
     * 消息模板配置
     */
    /*样式颜色*/
    public static final String COLOR ="#57854C";
    /*跳转路径*/
    public static final String RESULTURL ="www.baidu.com";

    /**
     * 消息发送模板
     * Map<String,Object> map = new HashMap<String, Object>();
     * map.put("first","经办人,您好,您提交的材料审核不通过.");  //必填项
     * map.put("keyword1","年检申报");                          //keyword个数看需求添加
     * map.put("keyword2","2019-05-31");
     * ....
     * map.put("keyword3","都是个顶个");
     * map.put("remark","审批意见:不同意");                           //必填项
     * @param openid
     * @param map
     */
    public void sendWord(String openid, Map<String,Object> map){
        ApiConfig apiConfig = new ApiConfig(Global.getConfig("ajz.fw.appId"), Global.getConfig("ajz.fw.secret"));
        TemplateMsgAPI templateMsgAPI = new TemplateMsgAPI(apiConfig);
        TemplateMsg templateMsg = new TemplateMsg();
        templateMsg.setTouser(openid);
        templateMsg.setTemplateId(Global.getConfig("ajz.fw.modelId"));//消息模板id

        //组装模板
        Map<String, TemplateParam> templateMsgMap = new HashMap<String, TemplateParam>();
        TemplateParam templateParam = null;
        String remark = "";

        //对需要组装的进模板的map进行遍历
        for(String key:map.keySet()){
            templateParam = new TemplateParam();
            templateParam.setColor(COLOR);
            templateParam.setValue(map.get(key).toString());
            templateMsgMap.put(key,templateParam);

            if("remark".equals(map.get(key).toString())){
                remark = key;
            }
        }

        templateMsg.setData(templateMsgMap);

//        String url1 = "" + "?content=" + remark;
        templateMsg.setUrl(RESULTURL);

        //发送模板
        SendTemplateResponse sendTemplateResponse =  templateMsgAPI.send(templateMsg);
        System.out.println(sendTemplateResponse);
    }


}
